import { memo } from "react";
import { connect } from "react-redux";
import { authPassed } from "@/utils";

const AuthContainer = memo((props) => {
    let backup = props.backup || null,
        authCode = typeof props.code === 'string' ? props.code : (Object.prototype.toString.call(props.code) === '[object Object]' ? props?.code?.code : ''),
        permissions = props.permissions || [],
        pass = authPassed(authCode, permissions);

    return pass ? props.children : backup;
});

function mapStateToProps({main}) {
    return {permissions: main?.userInfo?.permissions || []};
}

export default connect(mapStateToProps, null)(AuthContainer)